package com.wzy.level.detail.detailServlet;

import com.wzy.Jdbc封装.pojo.News_category;
import com.wzy.Jdbc封装.pojo.News_detail;
import com.wzy.Jdbc封装.pojo.News_user;
import com.wzy.level.byPage.byPageDto.ByPage;
import com.wzy.level.category.categoryService.categoryService;
import com.wzy.level.category.categoryServiceImpl.categoryServiceImpl;
import com.wzy.level.detail.detailService.detailService;
import com.wzy.level.detail.detailServiceImpl.detailServiceImpl;
import com.wzy.level.user.userService.userService;
import com.wzy.level.user.userServiceImpl.userServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/pageManage")
public class pageManage extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int index = 0;
        int count = 4;
        String sindex = req.getParameter("index");

        if(sindex == null){
            index = 0;
        }else {
            index = Integer.parseInt(sindex)-1;
        }
        HttpSession session = req.getSession();
        session.setAttribute("index",index);

        String count1 = req.getParameter("count");
        if(count1 != null){
            count = Integer.parseInt((count1));
        }
        session.setAttribute("count",count);


        ByPage byPage = new ByPage((index)*count,count);

        PrintWriter writer = resp.getWriter();
        String scate = req.getParameter("cate");
        int cate = 0;
        if (scate != null){
            cate = Integer.parseInt(scate);
            if(cate ==1){
                detailService detail = new detailServiceImpl();

                List<News_detail> news_details = detail.selectByPage(byPage);
                session.setAttribute("news_detail",news_details);

//                req.getRequestDispatcher("/detail").forward(req,resp);
                writer.print(1);

            }else if(cate ==2){
                userService user = new userServiceImpl();
                List<News_user> news_users = user.selectByPage(byPage);
                session.setAttribute("news_users_byPage",news_users);
                req.getRequestDispatcher("/user").forward(req,resp);
//                writer.print(1);
            }else if(cate == 4){
                categoryService category = new categoryServiceImpl();
                List<News_category> news_categories = category.selectByPage(byPage);
                session.setAttribute("news_categories",news_categories);
                req.getRequestDispatcher("/category").forward(req,resp);
//                writer.print(1);

            }
        }
        writer.flush();
        writer.close();
//
//
//        detailService detail = new detailServiceImpl();
//
//        List<News_detail> news_details = detail.selectByPage(byPage);
//        session.setAttribute("news_detail",news_details);
//
//        req.getRequestDispatcher("/detail").forward(req,resp);
    }
}
